<template>
	<div>
		<el-table :data="tableData" stripe style="width: 100%">
			<el-table-column prop="name" label="考试名称" show-overflow-tooltip>
				<template slot-scope="{ row }">
					<span>{{ row.testExam.name }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="examDuration" label="试卷类型" width="80">
				<template slot-scope="{ row }">
					<span>
						{{ row.testExam.testPaper.paperType == 1 ? '普通试卷' : '随机试卷' }}
					</span>
				</template>
			</el-table-column>

			<el-table-column prop="examDuration" label="考试时长" width="80">
				<template slot-scope="{ row }">
					<span>{{ row.testExam.examDuration }} 分钟</span>
				</template>
			</el-table-column>
			<el-table-column prop="passScore" label="卷面总分" width="80">
				<template slot-scope="{ row }">
					<span>{{ toFixeds(row.testExam.testPaper.totalScore) }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="passScore" label="及格分" width="80">
				<template slot-scope="{ row }">
					<span>{{ toFixeds(row.testExam.passScore) }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="score" label="我的得分" width="80">
				<template slot-scope="{ row }">
					<p v-if="publishRes(row)">{{ row.score }}</p>
					<p v-else>未公布</p>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="我的考试时间" width="180"></el-table-column>

			<el-table-column label="操作" width="200">
				<template slot-scope="{ row }">
					<el-button-group>
						<el-button type="success" size="mini" :disabled="!publishRes(row)" @click="handleLookAnswer(row)">查看答案</el-button>
						<el-button type="primary" size="mini" :disabled="!publishRes(row)" @click="handleCollectError(row)">收藏错题</el-button>
					</el-button-group>
				</template>
			</el-table-column>
			<div slot="empty" class="emptyBg">
				<el-empty :image-size="200"></el-empty>
			</div>
		</el-table>
		<Pagination v-show="parseInt(pageObj.count) > 0" :total="parseInt(pageObj.count)" @change="getList" :page.sync="listQuery.page" :limit.sync="listQuery.limit" />
		<ExamAnswer />
		<Classify />
	</div>
</template>
<script>
import moment from 'moment';
export default {
	components: {
		ExamAnswer: () => import('./ExamAnswer.vue'),
		Classify: () => import('../Question/Classify.vue')
	},
	data() {
		return {
			listQuery: {
				page: 1,
				limit: 10,
				createBy: this.$store.getters.userId
			},
			tableData: [],
			pageObj: {}
		};
	},
	computed: {
		publishRes() {
			return function (v) {
				let now = Date.now();
				let { publishTime } = v.testExam;
				publishTime = moment(publishTime).valueOf();
				return now > publishTime;
			};
		}
	},
	mounted() {
		this.getList();
	},
	methods: {
		async getList() {
			let res = await this.$store.dispatch('testExamHisQUERY', this.listQuery);
			this.tableData = res.data;
			this.pageObj = res;
		},
		handleLookAnswer(row) {
			this.$bus.$emit('showExamAnswer', row);
		},
		handleCollectError(row) {
			let subjectIds = row.testPaperAnswerVOList.filter((v) => v.isRight != 'Y').map((v) => v.id);
			this.$store.dispatch('addTestSubjectsError', { subjectIds });
			this.successMsg('操作成功');
		}
	}
};
</script>
<style scoped lang="scss"></style>
